#include <regx52.h>




unsigned char aix_Y_scan() //0~4
{
	unsigned char i = 0;
	unsigned char b = 0x01;
	for( ;i<4;++i)
	{
		P1 |= b<<i;
		if((P1&0xF0) == 0xF0) 
		{
			return 4-i;
		}
	}
	return 0;
}

unsigned char key_matrix_scan() //0~16 
{
	unsigned char X = 0;//0~4
	unsigned char Y = 0;//0~4
	P1 = 0xF0;
	if(P1 != 0xF0) 
	{
		if(P1 == 0x70) 
		{
			X = 1;
			Y = aix_Y_scan();
		} else if(P1 == 0xB0) 
		{
			X = 2;
			Y = aix_Y_scan();
		} else if(P1 == 0xD0) 
		{
			X = 3;
			Y = aix_Y_scan();
		} else if(P1 == 0xE0) 
		{
			X = 4;
			Y = aix_Y_scan();
		}
	}
	if(X==0) return 0;
	return 4*(X-1)+Y;
}